package com.woke.adapter;

import java.util.ArrayList;
import java.util.List;

import cn.xl.xxl.imagecache.ImageLoader;

import com.zhongcai.online.R;
import com.example.woke1.ZhongchouchildActivity;
import com.example.woke1.ZhongzhoubuyActivity;
import com.woke.data.Datas_child;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Zhouchouchildadter extends BaseAdapter{
	private LayoutInflater mInflater;
	private List<Datas_child> datalist;
	private ImageLoader imageLoader;
	private ZhongchouchildActivity activity;
	private String id;

	public Zhouchouchildadter(LayoutInflater minflater, ArrayList<Datas_child> datalist,
			ImageLoader imageLoader, ZhongchouchildActivity activity, String id) {
		this.mInflater = minflater;
		this.datalist = datalist;
		this.imageLoader = imageLoader;
		this.activity = activity;
		this.id = id;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datalist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	class Holder {
		TextView mTpeople;
		TextView mTname;
		TextView mTmoney;
		TextView mTok;
		TextView mTdescrise;
		GridView mGridv;
		ImageView imgesinle;
		
	}
	
	class Myadter extends BaseAdapter{
		private ArrayList<String> list ;
		public Myadter(ArrayList<String> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = mInflater.inflate(R.layout.image_single, null);
				holder.imgesinle = (ImageView)convertView.findViewById(R.id.imageView_singel);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			imageLoader.DisplayImage("http://"+list.get(position), holder.imgesinle, false, false, false,0);
			return convertView;
		}
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(convertView == null){
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.item_adter_zhongchoucl, null);
			holder.mTmoney = (TextView)convertView.findViewById(R.id.item_zhchouchilid_mongey);
			holder.mTpeople = (TextView)convertView.findViewById(R.id.item_zhchouchilid_people);
			holder.mTok = (TextView)convertView.findViewById(R.id.item_zhchouchilid_ok);
			holder.mTname = (TextView)convertView.findViewById(R.id.item_zhchouchilid_name);
			holder.mTdescrise = (TextView)convertView.findViewById(R.id.item_zhchouchilid_descrise);
			holder.mGridv = (GridView) convertView.findViewById(R.id.item_zhchouchilid_gridview);
			convertView.setTag(holder);
		}else{
			 holder = (Holder) convertView.getTag();
		}
		
//		LayoutParams params = holder.mImage1.getLayoutParams();
//		params.width = getphonewihd / 5 * 2;
////		params.height = params.height;
//		holder.mImage1.setLayoutParams(params);
		final Datas_child data = datalist.get(position);
		
//		Data_zhongchou data = datalist.get(position);
		holder.mTname.setText("  "+data.getTitle());
		holder.mTmoney.setText("￥"+data.getMoney());
		holder.mTpeople.setText("已支持"+data.getCount()+"/人");
		holder.mTdescrise.setText(data.getContent());
		ArrayList<String> list = new ArrayList<String>();
		if(null != data.getImg() && !data.getImg().equals("")){
			list.add(data.getImg());
		}if(null != data.getImg1() && !data.getImg1().equals("")){
			list.add(data.getImg1());
		}if(null != data.getImg2() && !data.getImg2().equals("")){
			list.add(data.getImg2());
		}
		Myadter adapter = new Myadter(list);
		holder.mGridv.setAdapter(adapter);
		holder.mTok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity,ZhongzhoubuyActivity.class);
				intent.putExtra("img", data.img);
				intent.putExtra("title", data.title);
				intent.putExtra("money", data.money);
				intent.putExtra("content", data.content);
				intent.putExtra("id", id);
				intent.putExtra("childid", data.id);
				activity.startActivity(intent);
			}
		});
		return convertView;
	}
}
